/*
 * @Author: szx
 * @Date: 2022-03-27 12:55:01
 * @LastEditTime: 2022-03-27 13:03:20
 * @Description:
 * @FilePath: \leetcode\剑指offer\53\53.js
 */
/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 */
var search = function (nums, target) {
    let res = 0;
    for (let i of nums) if (i == target) res++;
    return res;
};

var search = function (nums, target) {
    let i = 0,
        j = nums.length - 1;
    let mid;
    while (i <= j) {
        mid = Math.floor((i + j) / 2);
        if (nums[mid] >= target) {
            j = mid - 1;
        } else {
            i = mid + 1;
        }
    }
    const start = i;
    j = nums.length - 1;
    while (i <= j) {
        mid = Math.floor((i + j) / 2);
        if (nums[mid] > target) {
            j = mid - 1;
        } else {
            i = mid + 1;
        }
    }
    return i - start;
};
console.log(search([5, 7, 7, 8, 8, 10], 8));
